package com.cn.web.controller.api;

import cn.com.allunion.common.AllUnionResult;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.pojo.Smsbusiness;
import cn.com.allunion.sms.service.mybatis.SmsbusinessService;
import com.cn.web.bean.BusinessResult;
import com.cn.web.bean.PageViewResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对接业务管理控制器
 * Created by YangJie on 2016/4/20.
 */
@Controller
@RequestMapping("/business")
public class BusinessController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class) ;

    @Autowired
    SmsbusinessService smsbusinessService;

    @Value("${maxResult}")
    int pageSize ;

    /**
     * 新增对接业务
     * @param businessName 业务名称
     * @param businessCode 业务代码
     * @param businessJoinNum 业务接入号
     * @param businessSignature 业务签名
     * @param businessDesc 业务描述
     * @param status 状态码 0不可用， 1正常， 2测试
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestParam("businessName") String businessName,
                         @RequestParam("businessCode") String businessCode,
                         @RequestParam("businessJoinNum") String businessJoinNum,
                         @RequestParam("businessSignature") String businessSignature,
                         @RequestParam("businessDesc") String businessDesc,
                         @RequestParam("status") String status) {
        if (StringUtils.isBlank(businessName) || StringUtils.isBlank(businessCode)
                || StringUtils.isBlank(businessJoinNum) || StringUtils.isBlank(businessSignature) || StringUtils.isBlank(status)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "参数异常") ;
        }

        if (!("-1".equals(status) || "0".equals(status) || "1".equals(status))) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "状态码参数异常") ;
        }

        // 根据业务代码查找业务
        Smsbusiness smsbusiness = smsbusinessService.findBussinessObj(businessCode) ;

        if (null != smsbusiness) { // 业务代码已经存在
            return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "业务代码已经存在");
        }

        // 新增业务
        String result = smsbusinessService.addNewBusiness(businessName, businessDesc, businessCode,
                status, businessJoinNum, businessSignature);

        if ("success".equals(result)) {
            return AllUnionResult.ok() ;
        }

        return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部业务逻辑异常") ;
    }

    /**
     * 更新对接业务信息
     * @param businessId 业务id
     * @param businessName 业务名称
     * @param businessCode 业务代码
     * @param businessJoinNum 业务接入号
     * @param businessSignature 业务签名
     * @param businessDesc 业务描述
     * @param status 状态码 0不可用， 1正常， 2测试
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestParam("businessId") String businessId,
                         @RequestParam("businessName") String businessName,
                         @RequestParam("businessCode") String businessCode,
                         @RequestParam("businessJoinNum") String businessJoinNum,
                         @RequestParam("businessSignature") String businessSignature,
                         @RequestParam("businessDesc") String businessDesc,
                         @RequestParam("status") String status) {

        if (StringUtils.isBlank(businessId) || StringUtils.isBlank(businessName) || StringUtils.isBlank(businessCode)
                || StringUtils.isBlank(businessJoinNum) || StringUtils.isBlank(businessSignature) || StringUtils.isBlank(status)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "参数异常") ;
        }

        if (!("-1".equals(status) || "0".equals(status) || "1".equals(status))) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "状态码参数异常") ;
        }

        // 根据业务ID查找业务信息
        Smsbusiness smsbusiness = smsbusinessService.findbyId(businessId);

        if (null == smsbusiness) {
            return AllUnionResult.build(HttpStatus.NOT_FOUND.value(), "无此业务ID对应的信息") ;
        }

        try {
            String result = smsbusinessService.updateBusiness(businessId, businessName, businessDesc, businessCode,
                    status, businessJoinNum, businessSignature);

            if ("success".equals(result)) {
                return AllUnionResult.ok();
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }

        return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部业务逻辑异常") ;
    }

    /**
     * 根据业务id获取业务信息
     * @param businessId 业务id
     */
    @RequestMapping(value = "/get/{businessId}", method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable("businessId") String businessId) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[businessId: {}]", businessId);
        }
        if (StringUtils.isBlank(businessId)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "业务ID参数异常");
        }

        Smsbusiness smsbusiness = smsbusinessService.findbyId(businessId);

        if (null == smsbusiness) {
            return AllUnionResult.build(HttpStatus.NOT_FOUND.value(), "无此业务ID对应的信息");
        }

        // 转化为返回的结果集对象
        BusinessResult businessResult = BusinessResult.convert(smsbusiness);

        return AllUnionResult.ok(businessResult) ;
    }

    /**
     * 分页获取业务信息列表
     * @param pageCurrent 当前页码
     */
    @RequestMapping(value = "/scroll/{pageCurrent}", method = RequestMethod.GET)
    @ResponseBody
    public Object scroll(@PathVariable("pageCurrent") int pageCurrent) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[pageCurrent: {}]", pageCurrent);
        }
        if (pageCurrent <= 0) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "当前页码参数异常");
        }

        QueryResult<Smsbusiness> queryResult = smsbusinessService.findAllSmsbusiness(pageCurrent, pageSize);

        // 转化为返回的结果集对象
        PageViewResult<BusinessResult> pageViewResult = new PageViewResult<>(pageSize, pageCurrent);
        pageViewResult.setQueryResult(BusinessResult.convert(queryResult));

        return AllUnionResult.ok(pageViewResult) ;
    }

    /**
     * 获取所有业务信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list() {
        List<Smsbusiness> smsbusinessList = smsbusinessService.findAllSmsBusiness() ;

        return AllUnionResult.ok(BusinessResult.convert(smsbusinessList)) ;
    }
}
